package com.hurui.xiaoming.fragment.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.about.AboutActivity;
import com.hurui.xiaoming.activity.applicationagent.ApplicationAgentActivity;
import com.hurui.xiaoming.activity.detailedlist.DetailedListActivity;
import com.hurui.xiaoming.activity.me.MeInfoActivity;
import com.hurui.xiaoming.activity.me.MyProfitActivity;
import com.hurui.xiaoming.activity.opinion.OpinionActivity;
import com.hurui.xiaoming.bean.Li_Personal_Result;
import com.hurui.xiaoming.fragment.BaseFragment;
import com.hurui.xiaoming.myconfig.PamaryKeys;
import com.hurui.xiaoming.utils.WriteReadSharedPrefs;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hurui on 2017/7/13.
 */

public class MeFragment extends BaseFragment {
    private Li_Personal_Result body;
    private String TAG=MeFragment.class.getSimpleName();
    private View rootView ;
      private Map<String ,String> map;
    private LinearLayout goto_loan_lv ,goto_repay_lv ,
            goto_overdue_lv ,goto_cleared_lv ,
            goto_agent_lv , goto_opinion_lv,
            goto_about_lv ,goto_profit_lv;

    private RelativeLayout goto_me_info_rv,my_order ;
    private ImageView user_sex;
    private TextView nick_name,refreeName,ui_limit,profit,accountmanager;
    private ImageView ui_img;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        if(rootView == null){
            rootView = inflater.inflate(R.layout.fragment_me ,null);
            map=new HashMap<>();
        }
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        initView();
        initAction();
    }

    @Override
    public void initView() {
        goto_loan_lv = (LinearLayout) rootView.findViewById(R.id.goto_loan_lv);
        goto_repay_lv = (LinearLayout) rootView.findViewById(R.id.goto_repay_lv);
        goto_overdue_lv = (LinearLayout) rootView.findViewById(R.id.goto_overdue_lv);
        goto_cleared_lv = (LinearLayout) rootView.findViewById(R.id.goto_cleared_lv);
        goto_agent_lv = (LinearLayout) rootView.findViewById(R.id.goto_agent_lv);
        goto_opinion_lv = (LinearLayout) rootView.findViewById(R.id.goto_opinion_lv);
        goto_about_lv  = (LinearLayout) rootView.findViewById(R.id.goto_about_lv);
        goto_me_info_rv = (RelativeLayout) rootView.findViewById(R.id.goto_me_info_rv);
        goto_profit_lv = (LinearLayout) rootView.findViewById(R.id.goto_profit_lv);
        ui_img = ((ImageView) rootView.findViewById(R.id.ui_img));
        user_sex = (ImageView) rootView.findViewById(R.id.user_sex);
        nick_name = (TextView) rootView.findViewById(R.id.nick_name);
        refreeName = (TextView) rootView.findViewById(R.id.referee_name);
        ui_limit = ((TextView) rootView.findViewById(R.id.ui_limit));
        my_order= (RelativeLayout) rootView.findViewById(R.id.user_qingdan);
        profit= (TextView) rootView.findViewById(R.id.profit);
        accountmanager= (TextView) rootView.findViewById(R.id.accountmanager);
        rootView.findViewById(R.id.out_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存用户上次登录时间
                WriteReadSharedPrefs.outUser(mContext);
                apiSevice.onHttp_outLogin().enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        onFinish();
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        onFinish();
                    }
                });
            }
        });
    };


    public void onSuccess(Li_Personal_Result result) {

            Li_Personal_Result.DataBean data = result.getData();
        if (data!=null) {
            //性别
            if (data.getUi_sex() != null) {
                if (data.getUi_sex().toString().equals("1")) {
                    user_sex.setBackgroundResource(R.mipmap.man);
                } else {
                    user_sex.setBackgroundResource(R.mipmap.women);
                }
            }
            //昵称
            if (data.getUi_name()!=null) {
                nick_name.setText(data.getUi_name().toString());
            }
            //推荐人
            if (data.getUi_tjrid()!=null){
                refreeName.setText("推荐人："+data.getTjr_name().toString());
            }

            //可用额度
            if (data.getUi_limit()!=null){
                ui_limit.setText(data.getUi_limit());
            }
            //我的收益
                if (data.getProfit() != null){
                    profit.setText(data.getProfit());
                }
            //客户经理
            if (data.getJl_name()!=null){
               accountmanager.setText(data.getJl_name().toString());
            }
            //用户头像
            if (data.getUi_img()!=null){
                Glide.with(mContext).load("http://"+data.getUi_img().trim()).into(ui_img);
            }
        }
    }

    @Override
    public void initAction() {
        //WriteReadSharedPrefs.readUserID(mContext)+""
        map.put(PamaryKeys.UI_ID, WriteReadSharedPrefs.readUserID(mContext)+"");
        apiSevice.onHttpPostPersonal(map).enqueue(new Callback<Li_Personal_Result>() {


            @Override
            public void onResponse(Call<Li_Personal_Result> call, Response<Li_Personal_Result> response) {

                if (response.body().getStatus().equals("success")&& response.body().getData()!=null){
                    onSuccess(response.body());
                    body = response.body();
                    Log.e("wlx2222222", "xcxc: "+ body.getData().getProfit() );
                }
            }

            @Override
            public void onFailure(Call<Li_Personal_Result> call, Throwable t) {
                onHttpFail(0,t.getMessage());
            }
        });
        my_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startDetailed(0);
            }
        });
        goto_loan_lv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startDetailed(1);
            }
        });

        goto_repay_lv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startDetailed(2);
            }
        });

        goto_overdue_lv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startDetailed(3);
            }
        });

        goto_cleared_lv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startDetailed(4);
            }
        });

        //申请代理
        goto_agent_lv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, ApplicationAgentActivity.class);
                intent.putExtra(PamaryKeys.DATABEAN,body.getData());
                onOpen(intent);
            }
        });

        goto_opinion_lv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onOpen(new Intent(mContext , OpinionActivity.class));
            }
        });

        goto_about_lv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                onOpen(new Intent(mContext, AboutActivity.class));
            }
        });

        goto_me_info_rv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, MeInfoActivity.class);
                intent.putExtra(PamaryKeys.DATABEAN,body.getData());
                onOpen(intent);
            }
        });
        goto_profit_lv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (body.getData().getProfit()!=null)
                {
                    Intent intent = new Intent(mContext, MyProfitActivity.class);
                    intent.putExtra(PamaryKeys.DATABEAN,body.getData());
                    onOpen(intent);
                }
            }
        });

    }
    private void startDetailed(int index){
        Intent intent = new Intent(getActivity() , DetailedListActivity.class);
        intent.putExtra(PamaryKeys.state_flags , index );
        onOpen(intent);
    }
}
